package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.Settings;
import com.tanhua.server.service.SettingsService;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;

    /**
     * 读取用户通用设置
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){
        //获取当前用户id和phone
        Long userId = UserHolder.getUserId();
        String phone = UserHolder.getMobile();

        SettingsVo vo = settingsService.readSettings(userId,phone);

        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题 - 保存或更新
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        //获取陌生人问题
        String content = (String) map.get("content");

        settingsService.saveQuestion(content);

        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置 - 保存或更新
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity  notificationsSetting(@RequestBody Settings settings){
        //为settings对象设置userID
        settings.setUserId(UserHolder.getUserId());

        settingsService.saveNotificationsSetting(settings);

        return ResponseEntity.ok(null);
    }


    /**
     * 黑名单 - 翻页列表
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity  blacklist(@RequestParam(defaultValue = "1")int page,
                                     @RequestParam(defaultValue = "10") int size){

        PageResult pageResult = settingsService.blacklist(page,size);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 黑名单 - 移除
     * @param uid
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity  removeBlackList(@PathVariable("uid") Long uid){

        settingsService.removeBlackList(uid);

        return ResponseEntity.ok(null);
    }

}
